package com.project.basis.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import jp.wasabeef.glide.transformations.BitmapTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

object GlideUtilsKt {

    private fun getGlide(context: Context): RequestManager? {
        return try {
            Glide.with(context)
        } catch (e: IllegalArgumentException) {
            LogUtils.e(e.message!!)
            null
        }
    }

    private fun getGlide(view: View): RequestManager? {
        return try {
            Glide.with(view)
        } catch (e: IllegalArgumentException) {
            LogUtils.e(e.message!!)
            null
        }
    }

    // 加载图片
    fun loadPicUrl(imageView: AppCompatImageView, url: String) {
        getGlide(imageView)?.load(url)?.into(imageView)
    }

    // 带默认图加载
    fun loadPicUrl(imageView: AppCompatImageView, url: String, drawable: Int) {
        getGlide(imageView)?.load(url)?.apply(
            RequestOptions().placeholder(drawable)
        )?.into(imageView)
    }

    // 加载圆形图片
    fun loadCirclePicUrl(imageView: AppCompatImageView, url: String) {
        getGlide(imageView)?.load(url)?.apply(RequestOptions().circleCrop())?.into(imageView)
    }

    // 加载带默认图圆形图片
    fun loadCirclePicUrl(imageView: AppCompatImageView, url: String, drawable: Int) {
        getGlide(imageView)?.load(url)?.apply(
            RequestOptions().circleCrop()
                .placeholder(drawable)
        )?.into(imageView)
    }

    // 加载本地图片
    fun loadPicRes(imageView: AppCompatImageView, drawable: Int) {
        getGlide(imageView)?.load(drawable)?.into(imageView)
    }

    // 获取图片Bitmap
    fun getUrlBitmap(context: Context, url: String, callback: SimpleCallback<Bitmap>) {
        getGlide(context)?.asBitmap()?.load(url)?.into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                callback.onCallback(resource)
            }

            override fun onLoadCleared(placeholder: Drawable?) {

            }
        })
    }

    // 加载图片并返回bitmap
    fun loadPicUrl(imageView: AppCompatImageView, url: String, callback: SimpleCallback<Bitmap>) {
        getGlide(imageView)?.asBitmap()?.load(url)?.addListener(object : RequestListener<Bitmap> {
            override fun onLoadFailed(
                e: GlideException?,
                model: Any,
                target: Target<Bitmap>,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }

            override fun onResourceReady(
                resource: Bitmap,
                model: Any,
                target: Target<Bitmap>,
                dataSource: DataSource,
                isFirstResource: Boolean
            ): Boolean {
                callback.onCallback(resource)
                return false
            }

        })?.into(imageView)
    }

    // 缓存图片
    fun cachePicUrl(context: Context, url: String): RequestBuilder<Drawable>? {
        return getGlide(context)?.load(url)?.apply(RequestOptions().override(50, 50))
    }

    // 加载图片和缓存
    fun loadPicUrlAndCache(imageView: AppCompatImageView, url: String) {
        getGlide(imageView)?.load(url)?.thumbnail(cachePicUrl(imageView.context, url))
            ?.into(imageView)
    }

    // 加载其他样式图片
    fun loadTransformationPicUrl(
        imageView: AppCompatImageView,
        url: String,
        transformation: BitmapTransformation
    ) {
        getGlide(imageView)?.load(url)
            ?.apply(RequestOptions.bitmapTransform(transformation))?.into(imageView)
    }
}